package com.cloud.sys.controller;

import com.cloud.sys.common.ResponseWrapper;
import com.cloud.sys.config.YmlConfig;
import com.cloud.sys.entity.SysUser;
import com.cloud.sys.service.SysUserService;
import com.cloud.sys.utils.RedisUtils;
import com.cloud.sys.utils.RestTemplateUtil;
import com.cloud.sys.vo.PageUtil;
import com.cloud.sys.vo.SysUserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;
import sun.net.www.http.HttpClient;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * @author zxh
 * @Title: LoginController
 * @ProjectName sys-user
 * @Description: TODO
 * @date 2018/10/3117:00
 */

@RestController
@Slf4j
@Api(value = "用户管理", tags = "用户管理--登陆")
public class LoginController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private YmlConfig  ymlConfig;


    @PostMapping("/login")
    public ResponseWrapper login(@RequestBody SysUser user){
        user=sysUserService.login(user);
        if (user.getId()==null || "".equals(user.getId())){
            return ResponseWrapper.error("账号或密码不对");
        }else{
            SysUserVo sysUserVo=new SysUserVo("登录成功","200");
            sysUserVo.setToken(UUID.randomUUID().toString().replace("-", "").toLowerCase());
            List<String> list=new ArrayList<String>();
            list.add("admin");
            sysUserVo.setRole(list);
            sysUserVo.setUser(user);
            //存储token
            redisUtils.set(sysUserVo.getToken(), sysUserVo);
            //设置token时长
            redisUtils.expire(sysUserVo.getToken(),Long.parseLong(ymlConfig.getUserTime()));
            return ResponseWrapper.success(sysUserVo);
        }
    }
    @GetMapping ("/info/{token}")
    public ResponseWrapper info(@PathVariable("token") String token){
       SysUserVo userVo= (SysUserVo)redisUtils.get(token);
        return userVo!=null?ResponseWrapper.success(userVo):ResponseWrapper.error("查询失败");
    }

    @GetMapping ("/loginOut/{token}")
    public ResponseWrapper loginOut(@PathVariable("token") String token) {
        try {
            redisUtils.del(token);
            return ResponseWrapper.success("退出成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("退出活动异常", e.getMessage());
            return ResponseWrapper.error("系统异常，请联系管理员！！");
        }
    }


}
